/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UDPPanel.java
 *
 * Created on 2010-01-23, 19:14:03
 */

package efe.gui;

import efe.bean.EfePortValue;
import java.io.Serializable;
import java.util.Iterator;
import java.util.Vector;
import jpcap.packet.UDPPacket;

/**
 * Panel z nagłówkiem protokołu UDP.
 * @author Paweł
 */
public class UDPPanel extends javax.swing.JPanel implements Serializable {

    /** Creates new form UDPPanel */
    public UDPPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        srcPort = new efe.bean.EfePortValue();
        dstPort = new efe.bean.EfePortValue();
        efeEditorField1 = new efe.bean.EfeEditorField();
        efeEditorField2 = new efe.bean.EfeEditorField();
        efeEditorBlankField2 = new efe.bean.EfeEditorBlankField();
        efeEditorBlankField1 = new efe.bean.EfeEditorBlankField();

        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        setLayout(new java.awt.GridLayout(2, 2));

        efeEditorField1.setLabelText("Source Port");
        efeEditorField1.setValue(srcPort);
        add(efeEditorField1);

        efeEditorField2.setLabelText("Destination port");
        efeEditorField2.setValue(dstPort);
        add(efeEditorField2);

        efeEditorBlankField2.setText("Length");
        add(efeEditorBlankField2);

        efeEditorBlankField1.setText("Checksum");
        add(efeEditorBlankField1);
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Zwraca wektor serializowalnych wartości, które należy zapisać do pliku.
     * @return kolekcja wartości nagłówka
     */
    public Vector<Object> getValuesVector() {
        Vector<Object> v = new Vector<Object>();
        v.add(dstPort);
        v.add(srcPort);
        return v;
    }

    /**
     * Zapisuje dane z wektora do pól nagłówka.
     * @param v wektor wartości z elementami nagłówka w kolejności takiej samej
     * jak ta zwrócona przez metodę getValuesVector()
     */
    public void setValuesFromVector(Vector<Object> v) {
        Iterator<Object> i = v.iterator();
        dstPort = (EfePortValue) i.next();
        efeEditorField2.setValue(dstPort);
        srcPort = (EfePortValue) i.next();
        efeEditorField1.setValue(srcPort);
    }

    /**
     * Tworzy pakiet UDP z pól nagłówka, gotowy do tego żeby osadzić go
     * w warstwie sieciowej.
     * @return pakiet
     */
    public UDPPacket getUDPPacket() {
        // UDPPacket(int src_port, int dst_port) 
        return new UDPPacket(srcPort.getPort(), dstPort.getPort());
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private efe.bean.EfePortValue dstPort;
    private efe.bean.EfeEditorBlankField efeEditorBlankField1;
    private efe.bean.EfeEditorBlankField efeEditorBlankField2;
    private efe.bean.EfeEditorField efeEditorField1;
    private efe.bean.EfeEditorField efeEditorField2;
    private efe.bean.EfePortValue srcPort;
    // End of variables declaration//GEN-END:variables

}
